package sweng.cholesky.input;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import sweng.cholesky.BlackBoxTestInterface;
import sweng.cholesky.business.Matrix;
import sweng.cholesky.calculation.Cholesky;
import sweng.cholesky.calculation.Gauss;
import sweng.cholesky.calculation.Util;

public class Start implements BlackBoxTestInterface {

	public static void main(String args[]) {
		System.out.println("Herzlich Willkommen im Cholesky Zerlegungs Tool");
		System.out.println("Bitte geben sie den korrekten Pfad zur korrekten Matrixdatei an!");
		Matrix x = createMr();
		System.out.println(x);
		System.out.println("Bitte noch den Vektor einlesen!");
		Matrix p = createMr();
		x.print();
		p.print();
		System.out.println("Choleskyzerlegung Zeit: " + Cholesky.zerlege(x));
		Util.invert(x);
		System.out.println("Cholesky Solve: " + Cholesky.solve(x, null));
		
		System.out.println("-------------------------------------------");
		Gauss gauss = new Gauss();
		gauss.setMatrix(x.matrixIn);
		gauss.setVector(p.matrixIn);
		gauss.getResult();
	}

	public static Matrix createMr() {
		Matrix y;
		MatrixReader mr;
		do{
		 mr = new MatrixReader(getInput());
		} while (mr.isFehlerpassiert());
		 y = new Matrix(mr.getMatrix());

		return y;
	}

	public static String getInput() {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String line = null;
		try {
			while ((line = br.readLine()) != null) {
				return line;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return line;

	}

	@Override
	public double[] cholesky(String urlMatrix, String urlVector) {
		Matrix y;
		MatrixReader mr;
		do{
		 mr = new MatrixReader(urlMatrix);;
		} while (mr.isFehlerpassiert());
		 y = new Matrix(mr.getMatrix());
		double[] vektor =loadVector(urlVector);
		Cholesky.zerlege(y);
		Util.invert(y);
		return Cholesky.solve(y, vektor);
	}

	@Override
	public double[] gauss(String urlMatrix, String urlVector) {
		Matrix x;
		MatrixReader mr;
		do{
		 mr = new MatrixReader(urlMatrix);;
		} while (mr.isFehlerpassiert());
		 x = new Matrix(mr.getMatrix());
			Matrix p;
			do{
			 mr = new MatrixReader(urlMatrix);;
			} while (mr.isFehlerpassiert());
			 p = new Matrix(mr.getMatrix());
			Gauss gauss = new Gauss();
			gauss.setMatrix(x.matrixIn);
			gauss.setVector(p.matrixIn);
			return	gauss.getResult();
	}

	@Override
	public double[][] loadMatrix(String urlMatrix) {
		
		Matrix y;
		MatrixReader mr;
		do{
		 mr = new MatrixReader(urlMatrix);;
		} while (mr.isFehlerpassiert());
		 y = new Matrix(mr.getMatrix());
		return y.matrixIn;
	}

	@Override
	public double[] loadVector(String urlVector) {
		Matrix y;
		MatrixReader mr;
		do{
		 mr = new MatrixReader(urlVector);;
		} while (mr.isFehlerpassiert());
		 y = new Matrix(mr.getMatrix());
		 double[] vektor = new double[y.matrixIn.length];
		 for(int i=0;i<vektor.length;i++) {
			 vektor[i]=y.matrixIn[i][0];
		 }
		return vektor;
	}
}
